/**
 * 公共方法
 * modify by 24-06-13
 */
import { setGlobalData, getGlobalData } from '../global'
const adSerialStr = "1,4,6" //default

function filterSort(data){
  return data.sort((a, b) => {
    return Number(b.ext1) - Number(a.ext1);
  });
}
function filterTimer(pullCount=0){
  let adconfig = getGlobalData('adconfig')
  let pullUpConfig = adconfig?.quickAppConfig?.pullUpConfig;
  let list = pullUpConfig&&pullUpConfig.length>0?pullUpConfig:[{min:0,max:3,interval:0},{min:4,max:'Infinity',interval:1800}]
  let findItem = list.find(item=>{
    let max = ['Infinity','∞'].indexOf(item.max)>-1?'Infinity':item.max
    return pullCount>=item.min&&pullCount<=max
  })
  let result = findItem?.interval||0;
  // console.log('pullUpConfig:',pullUpConfig,'result:',result,Number(result))
  return Number(result)
}
function filterPull(){
  if(!getGlobalData('tagUser')){
    console.log('filterPull：不是标签用户')
    return false
  }
  if(getGlobalData('complaintState')) {
    console.log('filterPull：本次已投诉')
    return false
  }
  // 拉起次数判定
  let adconfig = getGlobalData('adconfig')
  let pullUpCnt = adconfig?.pullUpCnt;
  let pullCount = getGlobalData('pullCount')||0
  if(pullCount>=pullUpCnt){
    console.log('filterPull：满足拉起次数，不再拉起')
    return false
  }else{
    console.log('pullUpCnt(后台配置的值):',pullUpCnt,'pullCount(本地记录的值):',pullCount)
  }
  return true
}
function getAdSerial(type){
  let arr = []
  let adconfig = getGlobalData('adconfig')
  let advOrderConfig = adconfig?.advOrderConfig || adSerialStr;
  if(advOrderConfig!=''){
    let charArray = advOrderConfig.split(',')
    arr = charArray.map((char) => { 
      return parseInt(char)
    });
  }else{
    return null
  }
  let index = getGlobalData('adSerialIndex')
  let indexCurrent = index >= 0 && index< arr.length - 1? index+1 : 0
  // console.log('index',index,'indexCurrent',indexCurrent)
  let adSerial = arr[indexCurrent]
  setGlobalData('adSerialIndex',indexCurrent)
  console.log('adSerial',adSerial)
  return adSerial;
}
function filterAppluck(){
  const adconfig = getGlobalData('adconfig')
  const appLuck = adconfig?.appLuck || [];
  const advOrderConfig = adconfig?.advOrderConfig || adSerialStr;
  // console.log('filterAppluck-advOrderConfig:',advOrderConfig)
  if(appLuck.length==0){
    return {}
  }
  else{
    let appLuckSort = [...appLuck]
    filterSort(appLuckSort)
    // console.log('filterAppluck-appLuck:',JSON.stringify(appLuck))
    // console.log('filterAppluck-appLuckSort:',JSON.stringify(appLuckSort))
    let result ={}
    for(let i in appLuckSort){
      let item = appLuckSort[i];
      let params={
        n:item.positionName,
        v:item.luckId,
        source:item.luckSource,
        price:item.ext1
      }
      if(item.positionId==110){
        let arr = result.kp||[]
        arr.push(params)
        result.kp = arr
      }
      if(item.positionId==105){
        let arr = result.bannerTop||[]
        arr.push(params)
        result.bannerTop = arr
      }
      if(item.positionId==106){
        let arr = result.bannerBottom||[]
        arr.push(params)
        result.bannerBottom = arr
      }
      if(item.positionId==101&&advOrderConfig.includes(2)){
        let arr = result.cp||[]
        arr.push(params)
        result.cp = arr
      }
      if(item.positionId==102&&advOrderConfig.includes(3)){
        let arr = result.qp||[]
        arr.push(params)
        result.qp = arr
      }
      if(item.positionId==111&&advOrderConfig.includes(1)){
        let arr = result.nativeOne||[]
        arr.push(params)
        result.nativeOne = arr
      }
      if(item.positionId==109&&advOrderConfig.includes(4)){
        let arr = result.nativeFour||[]
        arr.push(params)
        result.nativeFour = arr
      }
      if(item.positionId==112&&advOrderConfig.includes(6)){
        let arr = result.nativeSix||[]
        arr.push(params)
        result.nativeSix = arr
      }
      if(item.positionId==104&&advOrderConfig.includes(5)){
        let arr = result.rv||[]
        arr.push(params)
        result.rv = arr
      }
    }
    console.log('filterAppluck result:',result, '|appLuck:',appLuck)
    return result
  }
}
export default {
  filterSort,
  filterTimer,
  filterPull,
  getAdSerial,
  filterAppluck
}
